<?php
/**
 * 搜窝
 *
 * @copyright 搜窝( www.souvo.com )
 * @package 前台个人主页
 * @version 1.0
 * @author wybwsk(wybwsk@qq.net)
 * @date 2011/5/7
 */
class PeopleAction extends FrontAction{
    protected function init() {
        $this->assign('user',$this->user);
        $this->assign('user_info',$this->user_info);
    }

    public function index() {
        if(intval(ACTION_NAME) === 0){
            alert('出错啦','main/index');
            exit();
        }
    }

    public function explore() {
        $this->display();
    }
    public function _empty() {
        $mid = intval(ACTION_NAME) ? intval(ACTION_NAME) : intval($this->user['mid']);
        $member_info = ($mid == $this->user['mid']) ? $this->user_info : M('member_info')->where(array('mid'=>$mid))->find();
        if(empty($member_info)) {
            alert('出错啦，找不到这个人','main/index');
            exit;
        }
        $tag = M('tag')->where(array('mid'=>$mid))->findAll();
        $link = M('link')->where(array('mid'=>$mid))->findAll();
        $comment = D('comment')->getComment($mid);
        $souvo_age = ceil((time()-$member_info['reg_time'])/86400);
        $this->assign('other_member',$mid);
        $this->assign('member_info',$member_info);
        $this->assign('link',$link);
        $this->assign('tag',$tag);
        $this->assign('comment',$comment);
        $this->assign('souvo_age',$souvo_age);
        $this->display('people');
    }
    public function clickNum() {
        if($this->ajax) {
            $db = M('link');
            $lid = intval($_POST['lid']);
            $click_num = $db->find($lid);
            $update_data['clicknum'] = $click_num['clicknum'] + 1;
            $db->data($update_data)->where(array('lid'=>$lid))->save();
            exit;
        }
    }

    public function tagAdd() {
        $this->user === false && exit('3');
        if($this->ajax !== false) {
            $data['tagname'] = htmlspecialchars($_POST['tagname']);
            empty($data['tagname']) && exit('2');
            $data['taghash'] = md5($tagname);
            $data['addtime'] = time();
            $data['mid'] = $this->user['mid'];
            $tid = M('tag')->data($data)->add();
            echo $this->outJsonMsg(0,'<span class="tag_del_show"><a href="'.U('main/search',array('tag'=>$data['tagname'])).'" title="'.$data['tagname'].'" target="_blank">'.$data['tagname'].'</a><span class="tag_del" style="color:red; display:none;cursor:pointer" tid="'.$tid.'">x</span></span>');
        }
        exit();
    }

    public function tagDel() {
        $this->user === false && exit('2');
        if($this->ajax !== false) {
            $data['tid'] = intval($_POST['tid']);
            $data['tid']===0 && exit('1');
            M('tag')->where($data)->delete();
        }
        exit('3');
    }

    public function collectAdd() {
        !$this->user && exit("999999999999");
        !$this->ajax && exit("999999999998");
        $data['collect_mid'] = intval($_POST['otmid']);
        ($data['collect_mid'] === 0 || $data['collect_mid'] == $this->user['mid']) && exit('1');
        $data['cgid'] = 1;
        $data['mid'] = $this->user['mid'];
        $data['add_time'] = time();
        if (M('collect')->data($data)->add()){
            $collect['collect_c'] = M('member_info')->find($data['collect_mid']);
            $collect['collect_c'] = $collect['collect_c']['collect_c'];
            $collect['collect_c']++;
            M('member_info')->data($collect)->where(array('mid'=>$data['collect_mid']))->save();
            echo $collect['collect_c'];
            unset($collect,$data);
            exit();
        }else{
            unset($data);
            exit('b');
        }
    }

    public function replyAdd() {
        !$this->user && exit('2');
        !$this->ajax && exit('1');
        $data['mid'] = intval($_POST['mid']);
        $data['cid'] = intval($_POST['cid']);
        $data['fcid'] = ($data['cid']) ? $data['cid'] : 0 ;
        unset($data['cid']);
        $data['reply_mid'] = $this->user['mid'];
        $data['content'] = htmlspecialchars($_POST['reply_content']);
        (empty($data['content']) || $data['content'] == '添加回复') && exit('3');
        mb_strlen($data['content'],'utf-8') > 140 && exit('4');
        $data['add_time'] = time();

        //回复时间间隔
        if($_SESSION['replay_time']) {
            $interval_time = time() - $_SESSION['replay_time'];
            $interval_time < 10 && exit('5');
        }
        session::set('replay_time',time());
        $db = M('comment');
        $cid = $db->data($data)->add();
        //加系统通知，没有子留言，只给页主发通知，有争对某人的子留言时，给页主和某人发通知。
        if($fcid==0) {

        }

        //留言新增,ajax返回,fcid=0 返回主留言，fcid !=0 返回子留言。
        if($cid) {
            $re=$db->query('SELECT a.*,b.nickname,b.small_img FROM souvo_comment as a,souvo_member_info as b where a.reply_mid=b.mid AND a.cid='.$cid);
            $re = $re[0];
            if($re['fcid']!=0) {
                $re='<table class="P-commentC-C">
                    <tr>
                        <td class="P-commentC-C-Author">
                            <p class="pt5 pl5 pr5 pb5"><a href="'.U('people/'.$re['reply_mid']).'" title="'.$re['nickname'].'" target="_blank"><img src="'.__APP__.'/resources/image/u/'.$re['small_img'].'" width=75 height=75 /></a></p>
                        </td>
                        <td>
                            <p class="pl10 pt10 pr10 pb5 gray"><a href="'.U('people/'.$re['reply_mid']).'" title="'.$re['nickname'].'" target="_blank">'.$re['nickname'].'</a> '.date('Y-m-d',$re['add_time']).' </p>
                            <p class="pl10">'.$re['content'].'</p>
                        </td>
                    </tr>
                </table>';
            }else{
                $re ='<li>
                        <table width="100%" style="border-bottom:1px dashed #eee; margin:10px 0;">
                            <tr>
                                <td width="75">
                                    <a target="_blank" title="'.$re['nickname'].'" href="'.U('people/'.$re['reply_mid']).'"><img width="75" height="75" src="'.__APP__.'/resources/image/u/'.$re['small_img'].'"></a>
                                </td>
                                <td>
                                    <p class="pt5 pl5 pr5 pb5">
                                        <a target="_blank" title="'.$re['nickname'].'" href="'.U('people/'.$re['reply_mid']).'">'.$re['nickname'].'</a>'.
                                        $re['content'].'
                                    </p>
                                    <p class="pl5 gray">'.date('Y-m-d',$re['add_time']).'</p>
                                </td>
                            </tr>
                        </table>
                    </li>';

            }
            echo $this->outJsonMsg(0,$re);
        }
        exit;
    }

    public function sendSms() {
        !$this->user && exit('3');
        $insert_data['content'] = htmlspecialchars($_POST['content']);

        //接收方ID.
        $insert_data['re_mid'] = intval($_POST['receive_id']);
        $insert_data['re_mid']==0 && exit('1');

        $tmp_data = M('member_info')->find($insert_data['re_mid']);
        $insert_data['re_nickname'] = $tmp_data['nickname'];
        $insert_data['mid'] = $insert_data['se_mid'] = $this->user['mid'];
        $insert_data['content'] = ($insert_data['content']) ? $insert_data['content'] : exit('2');
        $insert_data['add_time'] = $insert_data['update_time'] = time();

        // 1 '对方未看' 2 '对方已看' 3 '未看' 4'已看'
        $insert_data['rs_type'] = 1;
        $db = M('top_sitesms');
        $sid_a = $db->data($insert_data)->add();

        //生成对方收件夹站内信
        $insert_data['mid'] = $insert_data['re_mid'];
        $insert_data['re_mid'] = $this->user['mid'];
        $insert_data['re_nickname'] = $this->user_info['nickname'];
        $insert_data['rs_type'] = 3;
        $sid_b = $db->data($insert_data)->add();

        $db->data(array('relation_tsid'=>$sid_a))->where(array('tsid'=>$sid_b))->save();
        $db->data(array('relation_tsid'=>$sid_b))->where(array('tsid'=>$sid_a))->save();

        unset($db,$insert_data,$tmp_data);
        exit('4');
    }
}